package com.tac.service.impl;

import com.tac.common.result.Result;
import com.tac.common.thread.Thread;
import com.tac.dao.*;
import com.tac.pojo.*;
import com.tac.pojo.vo.Page;
import com.tac.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.text.Style;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductDao productDao;
    @Autowired
    private CommentDao commentDao;
    @Autowired
    private FootprintDao footprintDao;
    @Autowired
    private CollectionDao collectionDao;
    @Autowired
    private SaleDao saleDao;
    /**
     * 添加商品   图片还没有处理
     * @param product
     * @return
     */
    @Override
    public Result insert(Product product) {
        product.setUpdateUser(Thread.getId()+"");
        product.setCreateUser(Thread.getId()+"");
        product.setDeleteFlag(0);
        Integer insert = productDao.insert(product);
        if(insert>0){
            return Result.success(null,"success");
        }
        return  Result.error("添加失败");
    }

    /**
     * 根据id删除商品
     * @param id
     * @return
     */
    @Override
    public Result remove(Integer id) {
        Integer remove = productDao.remove(id);
        if(remove>0){
            return Result.success(null,"success");
        }
        return  Result.error("删除失败");
    }

    /**
     * //修改商品
     * @param product
     * @return
     */
    @Override
    public Result updateById(Product product) {
        product.setUpdateUser(Thread.getId()+"");
        product.setUpdateTime( new Date(System.currentTimeMillis()));
        Integer integer = productDao.updateById(product);
        if(integer>0){
            return Result.success(null,"success");
        }
        return  Result.error("修改失败");
    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @Override
    public Result<Product>  selectById(Integer id) {
        Product product = productDao.selectById(id);
        List<Comment> select = commentDao.select(id);
        product.setComments(select);
        //查看销量
        Date date = new Date(System.currentTimeMillis()-60*60*24*6*1000);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(date);

        List<Sale> sales = saleDao.selectByPid(id,format);
        product.setSales(sales);
        //判断是否已经添加 如果是就更新时间
        //先判断是否已经登录 登录才能用足迹和状态码
        try {
            if(Thread.getId()!=null) {
                Footprint footprint = footprintDao.selectByPidAndUid(id, Thread.getId());
                if (footprint != null) {
                    Footprint footprint1 = new Footprint();
                    footprint1.setProductId(id);
                    footprint1.setTime(LocalDateTime.now());
                    footprint1.setUserId(Thread.getId());
                    footprintDao.updateTime(footprint1);
                } else {
                    //如果不是则添加足迹
                    footprintDao.save(id, Thread.getId());
                }
                //判断是否已经收藏 给响应的状态码
                Collection collection = collectionDao.selectByPidAndUid(id, Thread.getId());
                if (collection == null) {
                    product.setCollectStatus(0);
                } else {
                    product.setCollectStatus(1);
                }
            }
        }catch (Exception ex){

        }
        return Result.success(product,"success");
    }

    /**
     * 查询所有分页查询
     * @return
     */
    @Override
    public Result<Page<List<Product>>>  selectAll(int page, int limit) {



        Integer TotalLimit = productDao.SelecteNmb();//总页码
        List<Product> products = productDao.selectAll((page-1)*limit,limit);
        for(Product product:products){
            //查看销量
            Date date = new Date(System.currentTimeMillis()-60*60*24*6*1000);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = simpleDateFormat.format(date);
            List<Sale> sales = saleDao.selectByPid(product.getId(),format);
            product.setSales(sales);
        }
        Page<List<Product>> listPage = Page.get(products, TotalLimit,limit);
        return Result.success(listPage,"success");
    }


    /**
     * 根据分类查询商品分页
     * @param id
     * @return
     */
    @Override
    public Result<Page<List<Product>>> selectAllByCategoryId(Integer id,int page, int limit) {
        Integer TotalLimit = productDao.SelecteNmbByCategory(id);
        List<Product> products = productDao.selectAllByCategoryId(id, page,limit);
        for(Product product:products){
            //查看销量
            Date date = new Date(System.currentTimeMillis()-60*60*24*6*1000);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = simpleDateFormat.format(date);
            List<Sale> sales = saleDao.selectByPid(product.getId(),format);
            product.setSales(sales);
        }
        Page<List<Product>> listPage = Page.get(products, TotalLimit, limit);
        return Result.success(listPage,"success");
    }

    /**
     * 按名称模糊查询商品分页按热门排序
     * @param name
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Result<Page<List<Product>>> selectByName(String name, int page, int limit) {
        productDao.updateHot(name);
        Integer TotalLimit = productDao.selectByNameAndCount(name);
        List<Product> products = productDao.selectByName(name, page, limit);
        for(Product product:products){
            //查看销量
            Date date = new Date(System.currentTimeMillis()-60*60*24*6*1000);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = simpleDateFormat.format(date);
            List<Sale> sales = saleDao.selectByPid(product.getId(),format);
            product.setSales(sales);
        }

        Page<List<Product>> listPage = Page.get(products, TotalLimit, limit);
        return Result.success(listPage, "success");
    }

  //  public Result<String> insertImg
}
